package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingProjectVo> page = nursingProjectMapper.getByPage(name,status);
        PageResponse<NursingProjectVo> pageResponse = new PageResponse(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    @Override
    public void add(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public NursingProjectVo getById(Integer id) {
        NursingProjectVo nursingProjectVo = nursingProjectMapper.getById(id);
        return nursingProjectVo;
    }

    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        status = status==1?0:1;
        nursingProjectMapper.changeStatus(id,status);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        //判断是否可以直接删除(是否正有项目在依赖)
        Integer online = nursingProjectPlanMapper.countProject(id);

        if(online!=0){
//            这里理论上应该有一个异常抛出
        }

        nursingProjectMapper.deleteById(id);
    }

    @Override
    public List<NursingProjectVo> getAll() {
        return nursingProjectMapper.getAll();
    }

}
